package com.rds.web.controller.pms;

import com.rds.common.annotation.Log;
import com.rds.common.constant.GenConstants;
import com.rds.common.core.controller.BaseController;
import com.rds.common.core.domain.AjaxResult;
import com.rds.common.core.page.TableDataInfo;
import com.rds.common.enums.BusinessType;
import com.rds.common.utils.SecurityUtils;
import com.rds.common.utils.StringUtils;
import com.rds.common.utils.poi.ExcelUtil;
//import com.rds.generator.service.GenTableServiceImpl;
import com.rds.system.domain.RdaType;
import com.rds.system.domain.RdaTypeAttribute;
import com.rds.system.service.IRdaTypeAttributeService;
import com.rds.system.service.IRdaTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

//import static com.rds.generator.util.GenUtils.arraysContains;

/**
 * 分类管理Controller
 *
 * @author rds
 * @date 2021-04-19
 */
@RestController
@RequestMapping("/pms/type")
public class RdaTypeController extends BaseController
{
    @Autowired
    private IRdaTypeService rdaTypeService;

    @Autowired
    private IRdaTypeAttributeService rdaTypeAttributeService;

//    @Autowired
//    private GenTableServiceImpl genTableService;

    /**
     * 查询分类管理列表
     */
    @PreAuthorize("@ss.hasPermi('pms:type:list')")
    @GetMapping("/list")
    public AjaxResult list(RdaType rdaType)
    {
        List<RdaType> list = rdaTypeService.selectRdaTypeList(rdaType);
        return AjaxResult.success(list);
    }

    /**
     * 导出分类管理列表
     */
    @PreAuthorize("@ss.hasPermi('pms:type:export')")
    @Log(title = "分类管理", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(RdaType rdaType)
    {
        List<RdaType> list = rdaTypeService.selectRdaTypeList(rdaType);
        ExcelUtil<RdaType> util = new ExcelUtil<RdaType>(RdaType.class);
        return util.exportExcel(list, "type");
    }

    /**
     * 获取分类管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('pms:type:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(rdaTypeService.selectRdaTypeById(id));
    }

    /**
     * 新增分类管理
     */
    @PreAuthorize("@ss.hasPermi('pms:type:add')")
    @Log(title = "分类管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody RdaType rdaType)
    {
        return toAjax(rdaTypeService.insertRdaType(rdaType));
    }

    /**
     * 修改分类管理
     */
    @PreAuthorize("@ss.hasPermi('pms:type:edit')")
    @Log(title = "分类管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody RdaType rdaType)
    {
        return toAjax(rdaTypeService.updateRdaType(rdaType));
    }

    /**
     * 删除分类管理
     */
    @PreAuthorize("@ss.hasPermi('pms:type:remove')")
    @Log(title = "分类管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(rdaTypeService.deleteRdaTypeByIds(ids));
    }


    /**
     * 查询类型字段属性列表
     */
    @PreAuthorize("@ss.hasPermi('pms:attribute:list')")
    @GetMapping("/attributeList")
    @Transactional
    public TableDataInfo attributeList(RdaTypeAttribute rdaTypeAttribute)
    {
        startPage();
        List<RdaTypeAttribute> list = rdaTypeAttributeService.selectRdaTypeAttributeList(rdaTypeAttribute);
        if(list != null && !list.isEmpty()){
            return getDataTable(list);
        }
//        else{
//            RdaType typeInfo= rdaTypeService.selectRdaTypeById(Long.valueOf(rdaTypeAttribute.getTypeid()));
//            List<RdaTypeAttribute> alist = rdaTypeAttributeService.selectAttributeByTableName(typeInfo.getTableName());
//            if(alist != null && !alist.isEmpty()){
//                Long userId = SecurityUtils.getUserId();
//                for(RdaTypeAttribute typeAttribute:alist){
//                    typeAttribute.setTypeid(rdaTypeAttribute.getTypeid());
//                    typeAttribute.setOwnerid(userId.intValue());
//                    typeAttribute.setUpdateBy(String.valueOf(userId));
//                    typeAttribute.setJavaField(StringUtils.toCamelCase(typeAttribute.getColumnName()));
//                    if (arraysContains(GenConstants.COLUMNTYPE_STR, typeAttribute.getColumnType()))
//                    {
//                        typeAttribute.setJavaType(GenConstants.TYPE_STRING);
//                    }
//                    else if (arraysContains(GenConstants.COLUMNTYPE_TIME, typeAttribute.getColumnType()))
//                    {
//                        typeAttribute.setJavaType(GenConstants.TYPE_DATE);
//                    }
//                    else if (arraysContains(GenConstants.COLUMNTYPE_NUMBER, typeAttribute.getColumnType()))
//                    {
//                        // 如果是浮点型 统一用BigDecimal
//                        String[] str = StringUtils.split(StringUtils.substringBetween(typeAttribute.getColumnType(), "(", ")"), ",");
//                        if (str != null && str.length == 2 && Integer.parseInt(str[1]) > 0)
//                        {
//                            typeAttribute.setJavaType(GenConstants.TYPE_BIGDECIMAL);
//                        }
//                        // 如果是整形
//                        else if (str != null && str.length == 1 && Integer.parseInt(str[0]) <= 10)
//                        {
//                            typeAttribute.setJavaType(GenConstants.TYPE_INTEGER);
//                        }
//                        // 长整形
//                        else
//                        {
//                            if(typeAttribute.getIsPk()==1){
//                                typeAttribute.setJavaType(GenConstants.TYPE_LONG);
//                            }else{
//                                typeAttribute.setJavaType(GenConstants.TYPE_INTEGER);
//                            }
//                        }
//                    }
//                    rdaTypeAttributeService.insertRdaTypeAttribute(typeAttribute);
//                }
//                List<RdaTypeAttribute> returnList = rdaTypeAttributeService.selectRdaTypeAttributeList(rdaTypeAttribute);
//                return getDataTable(returnList);
//            }
//            return getDataTable(new ArrayList<>());
//        }
        return getDataTable(new ArrayList<>());
    }

    /**
     * 导出类型字段属性列表
     */
    @PreAuthorize("@ss.hasPermi('pms:attribute:export')")
    @Log(title = "类型字段属性", businessType = BusinessType.EXPORT)
    @GetMapping("/attributeExport")
    public AjaxResult attributeExport(RdaTypeAttribute rdaTypeAttribute)
    {
        List<RdaTypeAttribute> list = rdaTypeAttributeService.selectRdaTypeAttributeList(rdaTypeAttribute);
        ExcelUtil<RdaTypeAttribute> util = new ExcelUtil<RdaTypeAttribute>(RdaTypeAttribute.class);
        return util.exportExcel(list, "attribute");
    }

    /**
     * 获取类型字段属性详细信息
     */
    @PreAuthorize("@ss.hasPermi('pms:attribute:query')")
    @GetMapping(value = "/attributeInfo/{id}")
    public AjaxResult getAttributeInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(rdaTypeAttributeService.selectRdaTypeAttributeById(id));
    }

    /**
     * 新增类型字段属性
     */
    @PreAuthorize("@ss.hasPermi('pms:attribute:add')")
    @Log(title = "类型字段属性", businessType = BusinessType.INSERT)
    @PostMapping("/addAttribute")
    public AjaxResult addAttribute(@RequestBody RdaTypeAttribute rdaTypeAttribute)
    {
        Long userId = SecurityUtils.getUserId();
        rdaTypeAttribute.setStatus(2);
        rdaTypeAttribute.setUpdateBy(String.valueOf(userId));
        return toAjax(rdaTypeAttributeService.insertRdaTypeAttribute(rdaTypeAttribute));
    }

    /**
     * 修改类型字段属性
     */
    @PreAuthorize("@ss.hasPermi('pms:attribute:edit')")
    @Log(title = "类型字段属性", businessType = BusinessType.UPDATE)
    @PutMapping("/editAttribute")
    public AjaxResult editAttribute(@RequestBody RdaTypeAttribute rdaTypeAttribute)
    {
        return toAjax(rdaTypeAttributeService.updateRdaTypeAttribute(rdaTypeAttribute));
    }

    /**
     * 删除类型字段属性
     */
    @PreAuthorize("@ss.hasPermi('pms:attribute:remove')")
    @Log(title = "类型字段属性", businessType = BusinessType.DELETE)
    @DeleteMapping("/removeAttribute/{ids}")
    public AjaxResult removeAttribute(@PathVariable Long[] ids)
    {
        return toAjax(rdaTypeAttributeService.deleteRdaTypeAttributeByIds(ids));
    }

    /**
     * 更新类型字段属性到数据表
     */
    @PreAuthorize("@ss.hasPermi('pms:attribute:remove')")
    @Log(title = "类型字段属性", businessType = BusinessType.UPDATE)
    @GetMapping("/updateSheet/{id}")
    public AjaxResult updateSheet(@PathVariable Long id)
    {
        RdaType typeInfo= rdaTypeService.selectRdaTypeById(id);
        if(typeInfo.getId()!=null){
            RdaTypeAttribute rdaTypeAttribute=new RdaTypeAttribute();
            rdaTypeAttribute.setTypeid(Math.toIntExact(typeInfo.getId()));
            List<RdaTypeAttribute> list = rdaTypeAttributeService.selectRdaTypeAttributeList(rdaTypeAttribute);

        }
        return toAjax(1);
    }

    /**
     * 生成代码
     */
    @PreAuthorize("@ss.hasPermi('pms:attribute:produce')")
    @Log(title = "生成代码", businessType = BusinessType.UPDATE)
    @GetMapping("/produceCode/{id}")
    public AjaxResult produceCode(@PathVariable Long id)
    {
        RdaType typeInfo= rdaTypeService.selectRdaTypeById(id);
//        int num = genTableService.produceCode(typeInfo.getTableName(),Math.toIntExact(id));
        return toAjax(1);
    }
}
